﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTest.Mock;

using aQuery;

namespace UnitTest
{
    [TestClass]
    public class ActionObserverTests
    {

        [TestMethod]
        public void ShouldWhenWorks()
        {
            ContaObservable c = new ContaObservable(0);
            c.Do(x => x.Withdraw(100)).When(x => x.Money > 100).StartWatching();
            c.Money = 150;

            System.Threading.Thread.Sleep(1000);
            Assert.AreEqual(c.Money, 50);
        }

        [TestMethod]
        public void ShouldStartAndStopWatchingWorks()
        {
            ContaObservable c = new ContaObservable(0);
            var watcher = c.Do(x => x.Print()).When(x => x.Money);

            c.Money = 100;
            System.Threading.Thread.Sleep(1000);

            Assert.IsTrue(String.IsNullOrEmpty(c.ResultOfPrint));

            watcher.StartWatching();

            c.Withdraw(10);
            System.Threading.Thread.Sleep(1000);

            Assert.AreEqual(c.ResultOfPrint, "90");

            watcher.StopWatching();

            c.Withdraw(10);
            System.Threading.Thread.Sleep(1000);

            Assert.AreEqual(c.ResultOfPrint, "90");
        }

        [TestMethod]
        public void ShouldntStartNewWatchingIfAlreadyIs()
        {
            ContaObservable c = new ContaObservable(0);
            var watcher = c.Do(x => x.Withdraw(100)).When(x => x.Money >= 100);

            watcher.Do(x => x.StartWatching()).Times(10).Execute();

            c.Money = 100;
            System.Threading.Thread.Sleep(1000);

            Assert.AreEqual(c.Money, 0);

            watcher.StopWatching();

            c.Money = 150;
            Assert.AreEqual(c.Money, 150);
        }

        [TestMethod]
        public void ShouldTreeExpressionWorks()
        {
            ContaObservable c = new ContaObservable(0);
            c.Do(x => x.Print()).When(x => x.Money >= 100 && x.Money < 200).StartWatching();
            c.Money = 150;
            
            System.Threading.Thread.Sleep(1000);

            Assert.AreEqual(c.ResultOfPrint, "150");
        }

    }
}
